<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <title>${empty course ? '添加' : '编辑'}课程</title>
    <link rel="stylesheet" href="https://www.layuicdn.com/layui-v2.6.8/css/layui.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://www.layuicdn.com/layui-v2.6.8/layui.js"></script>
    <style>
        .form-container {
            padding: 20px;
        }
    </style>
</head>
<body>
    <div class="form-container">
        <form class="layui-form" method="post" action="${pageContext.request.contextPath}/admin/course/save">
            <c:if test="${not empty course}">
                <input type="hidden" name="cno" value="${course.cno}">
            </c:if>
            
            <div class="layui-form-item">
                <label class="layui-form-label">课程号</label>
                <div class="layui-input-block">
                    <input type="text" name="cno" required lay-verify="required" 
                           placeholder="请输入课程号" class="layui-input" value="${course.cno}"
                           ${not empty course ? 'readonly' : ''}>
                </div>
            </div>
            
            <div class="layui-form-item">
                <label class="layui-form-label">课程名称</label>
                <div class="layui-input-block">
                    <input type="text" name="cname" required lay-verify="required" 
                           placeholder="请输入课程名称" class="layui-input" value="${course.cname}">
                </div>
            </div>
            
            <div class="layui-form-item">
                <label class="layui-form-label">学分</label>
                <div class="layui-input-block">
                    <input type="number" name="credit" required lay-verify="required|number" 
                           placeholder="请输入学分" class="layui-input" value="${course.credit}"
                           min="1" max="10">
                </div>
            </div>
            
            <div class="layui-form-item">
                <label class="layui-form-label">授课教师</label>
                <div class="layui-input-block">
                    <select name="tno" lay-verify="required">
                        <option value="">请选择教师</option>
                        <c:forEach items="${teachers}" var="teacher">
                            <option value="${teacher.tno}" ${course.tno == teacher.tno ? 'selected' : ''}>
                                ${teacher.tname}
                            </option>
                        </c:forEach>
                    </select>
                </div>
            </div>
            
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="courseForm">保存</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>

    <script>
        layui.use(['form', 'layer'], function(){
            var form = layui.form;
            var layer = layui.layer;
            
            form.on('submit(courseForm)', function(data){
                var index = parent.layer.getFrameIndex(window.name);
                
                $.ajax({
                    url: data.form.action,
                    type: 'POST',
                    data: data.field,
                    success: function(res){
                        if(res.code === 0){
                            parent.layer.msg('保存成功', {icon: 1});
                            parent.layer.close(index);
                            parent.location.reload();
                        } else {
                            layer.msg(res.msg || '保存失败', {icon: 2});
                        }
                    },
                    error: function(){
                        layer.msg('系统错误', {icon: 2});
                    }
                });
                return false;
            });
        });
    </script>
</body>
</html> 